<template>
  <div id="scenecoordinate">
    <el-row>
      <el-col :span="8">
        <el-row>
          <el-col :span="8">经度:</el-col>
          <el-col :span="16">
            <div id="divLongitude">{{ lng }}</div>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="8">
        <el-col :span="8">纬度:</el-col>
        <el-col :span="16">
          <div id="divLatitude">{{ lat }}</div>
        </el-col>
      </el-col>
      <el-col :span="8">
        <el-col :span="8">高程:</el-col>
        <el-col :span="16">
          <div id="divAltitude">{{ alt }}</div>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import { mapState } from 'vuex'
export default {
  name: 'SceneCoordinate',
  data() {
    return {
      lng: '0.00000',
      lat: '0.00000',
      alt: '0.00000',
      loadScene: false // 判断是否是第一次启动
    }
  },

  methods: {
    initialControls(viewer) {
      const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
      const ellipsoid = viewer.scene.globe.ellipsoid // 得到当前三维场景的椭球体
      handler.setInputAction((movement) => {
        // 通过指定的椭球或者地图对应的坐标系，将鼠标的二维坐标转换为对应椭球体三维坐标
        const cartesian = viewer.camera.pickEllipsoid(movement.endPosition, ellipsoid)
        if (cartesian) {
          const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
          const longitudeDegree = Cesium.Math.toDegrees(cartographic.longitude)
          const latitudeDegree = Cesium.Math.toDegrees(cartographic.latitude)
          this.lng = longitudeDegree.toFixed(5)
          this.lat = latitudeDegree.toFixed(5)
          const cartographic2 = Cesium.Cartographic.fromDegrees(longitudeDegree, latitudeDegree)
          const promise = Cesium.sampleTerrain(viewer.terrainProvider, 8, [cartographic2])
          Cesium.when(promise, (updatedPositions) => {
            if (updatedPositions.length > 0) {
              this.alt = updatedPositions[0].height ? updatedPositions[0].height.toFixed(5) : 0
            } else {
              this.alt = ''
            }
          })
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    }
  }
}
</script>
<style>
#scenecoordinate {
  width: 327px;
  z-index: 999;
  left: 10px;
  bottom: 10px;
  font-size: 13px;
  height: 22px;
  line-height: 22px;
  background-color: rgba(255, 255, 255, 0.6);
  border: 1px solid #dcdfe6;
  border-radius: 2px;
  position: absolute;
}

#divLongitude {
  padding: 0 5px;
}

#divLatitude {
  padding: 0 5px;
}

#divAltitude {
  padding: 0 5px;
}

.el-row {
  margin: 0 2px;
  padding: 0;
}
</style>
